<?php
/**
 * 管理员后台controll基类
 * @author Administrator
 *
 */
class CControllerManageBase extends CControllerBase{
	
	protected $_userID = 0;
	
	protected $_userName = "";
	
	protected $_imgDomain = "";
	protected $_manageDomain="";
	
	/**
	 * 线性导航
	 */
	private $_menu = array();
	
	/**
	 * 是否是超级管理员
	 * @var bool
	 */
	protected $_isAdmin = false;
	
	function __construct($controller, $action){
		parent::__construct($controller, $action);
		
		$this->_imgDomain = Config::get("imgDomain");
		$this->_manageDomain = Config::get("manageDomain");
		$this->checkLogin();
		
		$this->checkRight();
		
		$this->sysLog();
	}
	
	/**
	 * 验证登录
	 */
	private function checkLogin()
	{
		if(isset($_SESSION["manageInfo"]))
		{
			$this->_userID = $_SESSION["manageInfo"]["user_id"];
			$this->_userName = $_SESSION["manageInfo"]["username"];
			
			if($this->_userName == Config::get("adminUsername"))
			{
				$this->_isAdmin = true;
			}
		}
		else
		{
			if($this->getRequestData("isAjax","GET")==1 || $this->getRequestData("isAjax")==1)
    		{
    			echo "-100";
    			exit();
    		}
    		else
    		{
				$this->redirectUrl($this->_manageDomain);
    		}
		}
	}
	
	/**
	 * 验证登录用户权限
	 */
	private function checkRight()
	{
		$pageRight = $_SESSION["manageInfo"]["pageRight"];
		
		$hasRight = false;

		foreach ($pageRight as $row)
    	{
    		if(strtolower($row["identity_name"])==strtolower($this->_controller) && strtolower($row["right_ename"])==strtolower($this->_action))
    		{
    			$hasRight = true;
    			break;
    		}
    	}
    	
    	if(!$hasRight)
    	{
    		if($this->getRequestData("isAjax")==1 || $this->getRequestData("isAjax","GET")==1)
    		{
    			echo "-1000";
    			exit();
    		}
    		else
    		{
    			$this->sysMsg("没有权限",1);
    		}
    	}
	}
	
	/**
	 * 记录操作日志
	 */
	private function sysLog()
	{
		$stmtLog = new syslogModel();
		$arr = array("get"=>$_GET,"post"=>$_POST,"cookie"=>$_COOKIE);
		
		if(count($_POST)>0 || $this->_action=="del" || $this->_action=="batchDel")
		{
			$is_change = 1;
		}
		else{
			$is_change = 0;
		}
		
        //$get=json_encode($_GET);
        //$post=json_encode($_POST);
        //$desc="GET:".$get.",POST:".$post;
        $r = $stmtLog->insert(array(
        	"create_user"=>$this->_userName,
        	"description"=>json_encode($arr),
        	"create_time"=>date('Y-m-d H:i:s'),
        	"ip"=>CCommon::getClientIP(),
        	'module_identity'=>$this->_controller,
        	'is_change'=>$is_change,
        	'right_name'=>$this->_action),false
        );

	}
	
	/**
	 * 保存排序
	 */
	protected function updateSortNum($model)
	{
		foreach($_POST as $key=>$item)
		{
			$list = explode("-",$key);
			if(count($list)==2 && $list[0]=="txtSort")
			{
				$id = $list[1];
				
				$item = intval($item);
				
				$model->updateSortNum($id,$item);
			}
		}
	}
	
	/**
	 *设置页面标题
	 * @param string $title
	 */
	protected function setMenu($menu)
	{
		$this->_menu = $menu;
	}
	
	public function render($viewFile,$param=array(),$masterView="admin/common/master")
	{
		$this->setViewParam("imgDomain",$this->_imgDomain);
		$this->setViewParam("manageDomain",$this->_manageDomain);
		
		$menu = "";
		if($this->_menu)
		{
			if(is_array($this->_menu))
			{
				foreach($this->_menu as $item)
				{
					if(isset($item["href"]))
					{
						$menu .= " - <a href=\"".$item["href"]."\">".$item["text"]."</a>";
					}
					else
					{
						$menu .= " - <span id=\"search_id\" class=\"color2\">".$item["text"]."</span>";
					}
				}
			}
			else
			{
				$menu .= " - <span id=\"search_id\" class=\"color2\">".$this->_menu."</span>";
			}
		}
		
		$this->setViewParam("menu",$menu);
		
		if($masterView)
		{
			$ext = Config::get("viewSet.ext");
			$this->setViewParam("content","admin/".$viewFile.$ext);
			parent::render($masterView,$param);
		}
		else
		{
			parent::render("admin/".$viewFile,$param);
		}
	}
	
	/**
	 * 跳转到提示页面
	 * @param string $msgDetail 提示具体内容
	 * @param int $msgType 消息类型 0成功,1错误,2警告
	 * @param array $links 按钮链接和文字数组 ，格式array(array("text"=>"","href"=>""),array..)，如果为空，则自动显示一个“返回上一页”按钮,
	 * @param bool $auto_redirect 是否倒计时自动跳转
	 */
	protected function sysMsg($msgDetail, $msgType = 0, $links = array(), $autoRedirect = false)
	{
		$this->_validator = null;
		$this->setMenu("信息提示");
	    if (count($links) == 0)
	    {
	        $links[0]['text'] = "返回上一页";
	        $links[0]['href'] = 'javascript:history.go(-1)';
	    }

	    $array=array('ur_here'=>"系统消息", 'msg_detail'=>$msgDetail, 'msg_type'=>$msgType,
	    	'links'=>$links,'default_url'=>$links[0]['href'],'auto_redirect'=>$autoRedirect);

	    $this->render("common/message",$array);
	    exit;
	}

	
	/**
	 * 获取当前登录用户可以管理的社区,超级管理员可以管理全部，返回-1
	 */
	protected function getUserCommunityInfo()
	{
		if($this->_isAdmin)
		{
			return "-1";//管理员可以管理全部社区
		}
		$model=new baseUserModel();
		return $model->getUserCommunity($this->_userID);
	}
	
	/**
	 * 获取社区ID连接字符串
	 */
	protected function getUserCommunityID()
	{
		$cList = $this->getUserCommunityInfo();
		
		if($cList=="-1")
		{
			return "";
		}
		
		$cids = "";
		
		foreach($cList as $item)
		{
			$cids .= $item["community_id"].",";
		}
		
		if($cids)
		{
			$cids = substr($cids,0,strlen($cids)-1);
		}
		
		return $cids;
	}
}